package org.x.yuenovel.controller.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.x.yuenovel.enums.ResultCodeEnum;

/**
 * @author xuezhendong
 * @date 2022/4/20 9:19 下午
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultDTO<T> {

    private String code;

    private String msg;

    private T data;

    public static <T> ResultDTO<T> error(String code, String msg) {
        ResultDTO<T> res = new ResultDTO<>();
        res.setCode(code);
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public static <T> ResultDTO<T> error(ResultCodeEnum resultEnum) {
        ResultDTO<T> res = new ResultDTO<>();
        res.setCode(resultEnum.getCode());
        res.setMsg(resultEnum.getMsg());
        res.setData(null);
        return res;
    }

    public static <T> ResultDTO<T> error(ResultCodeEnum resultEnum, String msg) {
        ResultDTO<T> res = new ResultDTO<>();
        res.setCode(resultEnum.getCode());
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResultDTO<T> success(T data) {
        return new ResultDTO<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), data);
    }
}
